/*   
 * 文件名：TaxUserAction.java  
 * 版本：v1.0  
 * 创建时间：Jun 4, 2010 11:41:58 AM
 * 创建人：cAn   
 * Copyright WhTax Corporation 2010    
 * 版权所有   
 */
package com.hbyj.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;

import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;

import com.googlecode.jsonplugin.JSONUtil;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Departments;
import com.hbyj.hbm.vo.LogInfo;
import com.hbyj.hbm.vo.Members;
import com.hbyj.hbm.vo.MrBaseInf;
import com.hbyj.hbm.vo.datagridObj;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.JsonUtil;
import com.hbyj.util.OnLineUser;
import com.hbyj.util.PageClass;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author cAn
 * @since JDK 1.5
 * @version Jun 4, 2010 11:41:58 AM
 */
public class ManagerUserAction extends ActionSupport {
	private String jsonResult;
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public String UserLogin() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String ip = request.getRemoteAddr();
		OnLineUser onLineUsers = new OnLineUser();
		onLineUsers.setIp(ip);
		//查找所有用户
		List list = DbTools.findByProperty("Members", "name", username);
		if(list==null){
			jsonResult = "0";//数据库查询失败，请检查配置
		}
		//创建登录日志对象
		LogInfo log = new LogInfo();
		log.setUserId(username);
		Date date = new Date();
		Members info = null;
		//判断用户唯一，并且用户名密码相等
		if(list.size()==1 && ((Members)list.get(0)).getPassword().equalsIgnoreCase(password)){
			//用户第一次登录或者再次在同一个IP下登录 允许登录
			if(!onLineUsers.existUser(username)||ip.equals(onLineUsers.getOnLineUserIpByName(username))){
				log.setValid("是");
				log.setReason("成功登录");
				info = (Members)list.get(0);
				session.setAttribute("SysUser",(Members)list.get(0));
				session.setAttribute(info.getName(),onLineUsers);
				//System.out.println(onLineUsers.getCount());
				//Vector vc = onLineUsers.getOnLineUser();
				jsonResult = "1";//登录成功
			}else{
				jsonResult = "3";//用户在线
			}
			
		}else{
			
			log.setValid("否");
			log.setReason("用户名或密码错误，登录失败");
			jsonResult = "2";//用户名或密码错误，登录失败
		}
		/*try{
			 
			JsonConfig config  =   new  JsonConfig();
			//用过滤 
			config.setJsonPropertyFilter( new  PropertyFilter(){
			     public   boolean  apply(Object source, String name, Object value) {
			         if (name.equals("departments")||name.equals("memberRoles")) { //要过滤的areas ，Map对象中的
			             return   true ;
			        }  else  {
			             return   false ;
			        }
			    }
			});  
			//jsonResult =  JSONObject.fromObject(info,config).toString();
			
		}catch (Exception e) {
			System.out.println(e);
			// TODO: handle exception
		}*/
		//System.out.println(jsonResult);
		return SUCCESS;
	}
	
	/**
	 * 保存用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveUser() throws Exception {
		try {
			
			return "json";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	/**
	 * 更新用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateUser() throws Exception {
		try {
			
			return "json";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	/**
	 * author cuiyi
	 * time 2012-11-15
	 * disc 用户退出
	 * @return 
	 */
	public String UserExit() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Members mbInfo = (Members)session.getAttribute("SysUser");
		String userName = mbInfo.getName();
		session.removeAttribute("SysUser");
		session.removeAttribute(userName);
		return SUCCESS;
	}
}
